$(function(){
    okLoading.close();//关闭加载动画

    //加载页面JS代码时,请求数据并解析
    $.post("/getDesktopInfo",function(data){
        showDesktopInfo(data);
    });

    //购买课程，传递课程编号cid以及课时量num，价格price，购买者uid
    //并异步提交,前端提示数据
    $("#buyCourses").click(function(){
        $.post("/addToMyCourses",{"cid":$("#courseId").val(),
            "uid":$("#userId").val(),
            "classNum":$("#courseNum").val()},function () {
            $("#info").css("display","block");
            $("#msg").html("<p>&nbsp;</p><p>&nbsp;</p><p style='font-size: 42px;margin-top:-30px;'>购买成功</p>");
            window.setTimeout(function(){
                $("#info").css("display","none");
            },2500);
        });
    });
});

//将JSON数据进行解析,并在HTML可视化
function showDesktopInfo(data){
    showCoachList(data.coachList);
    showCourseList(data.courseList);
}

//解析教练信息
function showCoachList(coachList){
    var string = "";
    for(var i=0;i<coachList.length;i++){
        string = string +
            " <div class='col-sm-3 col-xs-12'>" +
            "<a href='javascript:;' onclick='showCoachByCid("+coachList[i].cid+")' data-toggle='modal' data-target='#coachModal'>"+
            "<div class='trainers-box'>"+
            "<figure>"+
            "<img src='images/"+coachList[i].sample+"' width='255px' height='255px'>"+
            "</figure>"+
            "<span>"+
            coachList[i].cname+
            "</span>"+
            "</div></a>"+
            "</div>";
    }
    console.log(string)
    $("#showCoachDiv").html(string);
}


//解析课程信息
function showCourseList(courseList){
    var string = "";
    for(var i=0;i<courseList.length;i++){
        string = string +
            "<div class='col-sm-4 col-xs-12'>"+
            "<div class='feature-box'>"+
            "<a href='javascript:;' onclick='showCourseByCid("+courseList[i].cid+")' data-toggle='modal' data-target='#myModal'>"+
            "<figure>"+
            "<img src='images/"+courseList[i].sample+"' width='350px' height='239.167px'>"+
            "<div class='time-box'>"+
            "<span class='date'><span>"+courseList[i].mold +"</span></span>"+
            "<span class='time'><span>教练:"+courseList[i].teacher +"</span></span>"+
            "</div>"+
            "</figure>"+
            "</a>"+
            "</div>"+
            "</div>";
    }
    console.log(string);
    $("#showCourseDiv").html(string);
}

//通过教练ID获取教练的信息(不包含学员信息以及业绩信息)
function showCoachByCid(cid){

}

//通过课程ID获取课程的信息
function showCourseByCid(cid){
    $.post("/getCourseByCid",{"cid":cid},function (data) {
        $("#courseId").val(data.cid);
        $("#samplePicture").attr("src","images/"+data.sample);
        $("#totalCoursePrice").val(data.price);
        $("#coursePrice").val(data.price);
        $("#courseName").text(data.cname);
        $("#courseTeacher").text(data.teacher);
        $("#courseVenue").text(data.venue)
        $("#courseHot").text(data.searchRanking);
        $("#courseLike").text(data.orderRanking);
        $("#courseNum").val(1);
    });
}


function add(){
    var num = parseInt($("#courseNum").val());
    var price = parseInt($("#coursePrice").val());
    $("#courseNum").val(num+1);
    $("#totalCoursePrice").val((num+1)*price);
}

function sub(){
    var num = parseInt($("#courseNum").val());
    var price = parseInt($("#coursePrice").val());
    if(num<=1){return false;}
    $("#courseNum").val(num-1);
    $("#totalCoursePrice").val((num-1)*price);
}

//实时监听课时数的变化，实现对总数值的改变

$("#courseNum").bind('input propertychange',function(){
    var price = parseInt($("#coursePrice").val());
    if(isNaN($(this).val())==false){
        $("#totalCoursePrice").val(parseInt($(this).val())*price);
    }else{
        $(this).val(1);
        $("#totalCoursePrice").val(price);
    }
});